# import time
# import atexit
#
#
# class CaseLogger:
#     """
#     测试用例日志记录器
#
#     增加测试用例执行日志上下文管理器，用于记录测试用例的执行情况
#     """
#
#     def __init__(self):
#         self.case_start_time = 0
#         self.step_count = 0
#         self.success_steps = 0
#         self.failed_steps = 0
#
#         self.total_tests = 0
#         self.total_success = 0
#         self.total_failed = 0
#         atexit.register(self.log_summary)
#
#     def start_test_case(self):
#         """开始记录测试用例"""
#         self.case_start_time = time.time()
#         self.step_count = 0
#         self.success_steps = 0
#         self.failed_steps = 0
#
#     def end_test_case(self):
#         """结束测试用例并打印结果"""
#         duration = time.time() - self.case_start_time
#         status = "通过" if self.failed_steps == 0 else "失败"
#         status_color = "\033[32m" if status == "通过" else "\033[31m"
#         reset_color = "\033[0m"
#
#         # print("\n" + "-" * 80)
#         print()
#         result_line = f"测试结果: {status_color}{status}{reset_color} | 步骤: {self.success_steps}/{self.step_count} | 耗时: {duration:.1f}s"
#         print(result_line)
#         print()
#
#         self.total_tests += 1
#
#         if status == "通过":
#             self.total_success += 1
#         else:
#             self.total_failed += 1
#
#     def stack_step(self, func):
#         """装饰器，用于统计测试用例步骤的数量"""
#         from functools import wraps
#
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             try:
#                 result = func(*args, **kwargs)
#                 # return result
#             except Exception as e:
#                 raise
#             else:
#                 self.step_count += 1
#                 self.success_steps += 1
#                 return result
#
#         return wrapper
#
#     def add_fail_steps(self, func):
#         """装饰器，用于统计测试用例步骤的数量"""
#         from functools import wraps
#
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             try:
#                 result = func(*args, **kwargs)
#                 # return result
#             except Exception as e:
#                 raise
#             else:
#                 self.step_count += 1
#                 self.failed_steps += 1
#                 return result
#
#         return wrapper
#
#     def log_summary(self):
#         """打印测试总结信息"""
#         print("\n" + "=" * 80)
#         print(f"测试总结: 总测试数: {self.total_tests} | 成功: {self.total_success} | 失败: {self.total_failed}")
#         print("=" * 80)
#
#     def __enter__(self):
#         self.start_test_case()
#         # 返回要提供给 `with` 语句块的对象
#         return self
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         if exc_type:
#             print(f"步骤  执行中出错: {exc_val}")  # {self.step_name}
#         else:
#             self.end_test_case()
#
#         # 返回 True 可以抑制异常，返回 False 会传递异常
#         return False
#
#
# case_logger = CaseLogger()
#
#
# class StatusLogger:
#     """状态日志记录器"""
#
#     def __init__(self, status="success"):
#         self.status = status
#
#     @case_logger.stack_step
#     def step(self, step_type, description, timestamp=None, duration=None):
#         """
#         打印步骤执行结果
#
#         Args:
#             step_type (str): 步骤类型，如"步骤"、"断言"等。
#             description (str): 步骤描述。
#             timestamp (str, optional): 步骤发生的时间戳，默认为当前时间。
#
#         """
#         status_map = {
#             "success": "✓",
#             "skip": "↷",
#             "fail": "✗",
#             "start": "→"
#         }
#         status_color_map = {
#             "success": "\033[32m",  # Green
#             "skip": "\033[33m",  # Yellow
#             "fail": "\033[31m",  # Red
#             "start": "\033[34m"  # Blue
#         }
#         reset_color = "\033[0m"
#
#         status_icon = status_map.get(self.status, ' ')
#         status_color = status_color_map.get(self.status, "\033[0m")
#         timestamp = timestamp if timestamp else time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#
#         # 如果有持续时间则打印
#         duration_str = f" (Duration: {duration:.2f}s)" if duration else ""
#
#         print()
#         print(f"[{timestamp}] {status_color}[{status_icon}] {step_type}: {description}{reset_color}{duration_str}")
#
#         if self.status != "success":
#             case_logger.add_fail_steps(self)
#
#     def log(self, action, target, *args):
#         """记录操作日志"""
#         status_map = {
#             "success": "✓",
#             "fail": "✗",
#             "start": "→"
#         }
#
#         status_icon = status_map.get(self.status, ' ')
#
#         print(f"[{status_icon}] {action}: {target}", *args)
#
#     def __set_name__(self, owner, name):
#         """
#         设置实例属性的名称。自动捕获属性名
#
#         Args:
#             name: 实例属性的名称
#         """
#         self.status = f"{name}"  # 生成私有属性名
#
#     def __get__(self, instance, owner):
#         """
#         获取实例属性时触发的方法。
#
#         Args:
#             instance: 拥有该描述符的实例（若通过类访问则为None）
#             owner: 拥有该描述符的类
#         """
#         if instance is None:
#             return self
#
#         return self
#
#
# class TestLogger:
#     """测试日志记录器"""
#
#     start = StatusLogger()
#     fail = StatusLogger()
#     success = StatusLogger()
#     skip = StatusLogger()
#
#     def __init__(self):
#         pass
#
#     @staticmethod
#     def print_header(title):
#         """打印测试标题"""
#         print(f"\n{'=' * 80}")
#         print(f"{title.center(76)}")
#         print(f"{'=' * 80}")
#
#
# test_logger = TestLogger()
#
# if __name__ == '__main__':
#     test_logger.start.log("登录", "http://www.baidu.com/login")
#     test_logger.success.log("<UNK>123<UNK>", "http://www.baidu.com/login")
